'''
多线程创建
'''
# #1
import csv
import json
import time
from threading import Thread
# def func():
#     for i in range(1000):
#         print(str(i) + "func")
#
# #2
# class MyThread(Thread):
#     def run(self) -> None:
#         for i in range(1000):
#             print(str(i) + "func")

# if __name__ == "__main__":
#     # t = Thread(target=func)
#     t = MyThread()
#     t.start() #状态开启，未必执行
#     for i in range(1000):
#         print(i)

'''
多进程
'''

from multiprocessing import Process
#
# def func1():
#     for i in range(1000):
#         time.sleep(0.01)
#         print(str(i) + "子进程")
#
# if __name__ == "__main__":
#     p = Process(target=func1)
#     p.start() #状态开启，未必执行
#     for i in range(1000):
#         time.sleep(0.01)
#         print(i)

'''
线程池
'''
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# def fn(name):
#     for i in range(1000):
#         print(name, i)
# if __name__ == '__main__':
#     #创建线程池
#     with ThreadPoolExecutor(50) as t:
#         for i in range(100):
#             t.submit(fn, name=f"线程{i}")
#     print(0)

'''
线程池爬取北京新发地数据
'''

import requests

url = "http://www.xinfadi.com.cn/getPriceData.html"
heards = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
    #溯源
    ,"Referer": "http://www.xinfadi.com.cn/priceDetail.html"
}
data = {
    "limit": "20",
    "current": "",
    "pubDateStartTime": "",
    "pubDateEndTime": "",
    "prodPcatid": "",
    "prodCatid": "",
    "prodName": ""
}
f = open("./文件/data.csv", mode="w", encoding="utf-8")
cw = csv.writer(f)
def fn(i):
    data["current"] = i
    resp = requests.post(url=url, headers=heards, data=data)
    for i in range(20):
        cw.writerow(str(resp.json()['list'][i].values()))
    print(type(resp.json()))
with ThreadPoolExecutor(50) as t:
    for i in range(1, 2):
        t.submit(fn, i)
    print(0)
